package edu.biwu.cookie02;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
/**
 * 创建Cookie,响应给浏览器,使用的是默认路径
 * 设置一个Cookie: gender=male,默认路径: /day07-cookie-session/path01/
 *   访问/day07-cookie-session/path01/getCookie01:  携带
 *   访问/day07-cookie-session/path01/path02/getCookie02: 携带
 *   访问/day07-cookie-session/getCookie03:  不携带
 *   如果请求的路径是cookie的路径 或者是 Cookie路径的子级路径才会携带这个cookie
 */
@WebServlet("/path01/setCookie01")
public class SetCookie01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向浏览器中写入一个gender=male的cookie
        // 该cookie使用的默认路径: /day07-cookie-session/path01
        // 完整访问路径(不包含协议,主机,端口号): /day07-cookie-session/path01/setCookie01
        // Cookie默认路径: 完整路径的最后一级路径前面的所有部分: /day07-cookie-session/path01/
        response.addCookie(new Cookie("gender","male"));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
